In this notebook, we show how to download CSISA_KVK trials dataset from dataverse and merge to soil variables needed for the causal random forest targeting.
# rm(list=ls()) # clear library(sp)library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
The CSISA-KVK trial data contains approximate GPS locations of the plots. We can use these to extract soil and climate variables that are then included in crop response function.
# Function to add Geo-variables library(sf)
Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(sp)library(rgdal)
Please note that rgdal will be retired by the end of 2023,
plan transition to sf/stars/terra functions using GDAL and PROJ
at your earliest convenience.
rgdal: version: 1.5-29, (SVN revision 1165M)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
Path to GDAL shared files: C:/Users/MMKONDIWA/OneDrive - CIMMYT/Documents/R/win-library/4.1/rgdal/gdal
GDAL binary built with GEOS: TRUE
Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
Path to PROJ shared files: C:/Users/MMKONDIWA/OneDrive - CIMMYT/Documents/R/win-library/4.1/rgdal/proj
PROJ CDN enabled: FALSE
Linking to sp version:1.4-6
To mute warnings of possible GDAL/OSR exportToProj4() degradation,
use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
Overwritten PROJ_LIB was C:/Users/MMKONDIWA/OneDrive - CIMMYT/Documents/R/win-library/4.1/rgdal/proj
library(terra)
terra 1.5.21
Attaching package: 'terra'
The following object is masked from 'package:rgdal':
project
The following object is masked from 'package:tidyr':
extract
The following object is masked from 'package:dplyr':
src
library(raster)
Attaching package: 'raster'
The following object is masked from 'package:dplyr':
select
CSISA_KVK$Latitude=as.numeric(CSISA_KVK$Latitude)CSISA_KVK=subset(CSISA_KVK,!(is.na(CSISA_KVK$Longitude)))CSISA_KVK=subset(CSISA_KVK,!(is.na(CSISA_KVK$Latitude)))# add_secondary_lcas <- function (df) {# # Remove duplicates and NAs in geo-coordinates# #df=subset(df,!(duplicated(df$Longitude)))# #df=subset(df,!(duplicated(df$Latitude)))# df$Longitude=as.numeric(df$Longitude)# df$Latitude=as.numeric(df$Latitude)# df=subset(df,!(is.na(df$Longitude)))# df=subset(df,!(is.na(df$Latitude)))# df_sp= SpatialPointsDataFrame(cbind(df$Longitude,df$Latitude),data=df,proj4string=CRS("+proj=longlat +datum=WGS84"))# df_sf=st_as_sf(df_sp)# # population=population(2020,05,path=tempdir())# population_geodata=terra::extract(population,vect(df_sf),fun=mean,df=TRUE)# elevationglobal_geodata=elevation_global(0.5,path=tempdir())# elevation_geodata=terra::extract(elevationglobal_geodata,vect(df_sf),fun=mean,df=TRUE)# Soilsand=soil_world("sand",depth=5,path=tempdir())# Soilsand_lds=terra::extract(Soilsand,vect(df_sf),fun=mean,df=TRUE)# Totalnitrogen=soil_world("nitrogen",depth=5,path=tempdir())# Totalnitrogen_lds=terra::extract(Totalnitrogen,vect(df_sf),fun=mean,df=TRUE)# soilsoc=soil_world("soc",depth=15,path=tempdir())# soilsoc_lds=terra::extract(soilsoc,vect(df_sf),fun=mean,df=TRUE)# # # Merge all soils and population# geodata_df <- list(population_geodata,elevation_geodata,Soilsand_lds,Totalnitrogen_lds,soilsoc_lds)# geodata_df=Reduce(function(x, y) merge(x, y, all=TRUE),geodata_df)# #geodata_df=return(data.frame(geodata_df))# write.csv(geodata_df,paste0("CSISA_KVK_geovariables",".csv"))# }# add_secondary_lcas(CSISA_KVK)library(rio)geovariables=import("CSISA_KVK_geovariables.csv")CSISA_KVK=cbind(CSISA_KVK,geovariables)
3 Climate variables
The geodata R package has aggregated rainfall and temperature variables. However, we need climate variables specific to the corresponding growing season.